package org.john.auth.server.controller;

import org.john.auth.client.annotation.IgnoreTokenValidate;
import org.john.auth.client.vo.UserLoginParam;
import org.john.auth.server.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.irm.util.UserSessionCookieHelper;
import com.irm.vo.UserSessionContext;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/sso")
public class SessionController {
	@Autowired
	private AuthService userSessionService;

	@IgnoreTokenValidate
	@PostMapping("/login")
	public ResponseEntity<String> login(@RequestBody UserLoginParam loginParam) {
		UserSessionContext userSessionContext = userSessionService.login(loginParam);
		String token = UserSessionCookieHelper.generateToken(userSessionContext);
		return ResponseEntity.ok(token);
	}

	@IgnoreTokenValidate
	@PostMapping("/logout")
	public ResponseEntity logout(@RequestBody String token) {
		userSessionService.logout(token);
		return ResponseEntity.ok().build();
	}

}
